const express = require('express')
const cookieParser = require('cookie-parser')
const bodyparser = require('body-parser')
const cookieSession = require('cookie-session')
const multer = require('multer')
const ejs = require('ejs')
const path = require('path')
const fs = require('fs')

const app = express();
const upload = multer({ dest: 'dest/' })

app.use(upload.any())
app.use(cookieParser('sd2iuozxcjkl3we1'))
app.use(bodyparser.urlencoded({ extended: false }))


// session
let keys = [];
for (let i = 0; i < 10000; i++) {
    keys.push('keys_' + Math.random())
}
app.use(cookieSession({
    name: 'vx_si',
    keys: keys,
    maxAge: 24 * 3600 * 1000
}))


app.get('/', function(req, res) {

    // console.log(req.query)

    // let _cookies = req.cookies
    // let cookies = req.signedCookies

    // let count = cookies.count ? +cookies.count + 1 : 0

    // res.cookie('count', count, {
    //     path: '/',
    //     maxAge: 180*24*3600*1000,
    //     signed: true
    // })

    // let session = req.session
    // session.count ? session.count ++ : (session.count = 1)
    // console.log(session)


    // console.log(req.body)

    // res.send('<h3>你的访问次数：'+ session.count +'</h3>')
    res.sendfile('./www/form.html')

});

app.post('/up', function(req, res) {
    console.log(req.body)
    console.log(req.files)

    let nName = req.files[0].path + path.parse(req.files[0].originalname).ext

    fs.rename(req.files[0].path, nName, function(err) {
        if (err) {
            res.send('error')
        } else {
            res.set('Content-Type', 'text/plain; charset=utf-8');
            res.send({ dd: 1263 })
        }
    })
})


// IE下对于文件选择不允许模拟点击!
app.listen('3001', function(err) {
    if (err) {
        console.log(err);
        return
    }

    console.log('app runing listen 3001');
})